package com.security.customer.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: TongRui乀
 * @Date: 2020/3/21 10:03
 * @description：
 */
@Slf4j
@Component("customerUsernamePasswordLoginSuccessHandler")
public class CustomerUsernamePasswordLoginSuccessHandler implements AuthenticationSuccessHandler {

    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {

        log.info("登录成功 -- CustomerUsernamePasswordLoginSuccessHandler");

        // 这里默认返回的是json数据
        // 同时在这里也可已决定是否是跳转页面  可以通过继承 SavedRequestAwareAuthenticationSuccessHandler 来实现非json返回的处理逻辑

        httpServletResponse.setContentType("application/json;charset=utf-8");
        httpServletResponse.getWriter().write(objectMapper.writeValueAsString(authentication));

    }
}
